package com.cy.cdas.service.impl;

import com.cy.cdas.dao.UserDao;
import com.cy.cdas.entity.User;
import com.cy.cdas.service.UserService;
import com.cy.cdas.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;


    @Override
    public ResultData add(User user) {
        int i = userDao.add(user);
        ResultData resultData = new ResultData();
        List list = new ArrayList();
        if(i>0){
            resultData.setCode(1);
            resultData.setMsg("成功");
            list.add(user);
            resultData.setData(list);

        }else{
            resultData.setCode(0);
            resultData.setMsg("失败");
            list.add(null);
            resultData.setData(list);
        }
        return resultData;
    }

    @Override
    public ResultData delete(List<Integer> ids) {

        int i = userDao.delete(ids);
        ResultData resultData = new ResultData();
        if(i>0){
            resultData.setCode(1);
            resultData.setMsg("成功");
            resultData.setData(null);

        }else{
            resultData.setCode(0);
            resultData.setMsg("失败");
            resultData.setData(null);
        }
        return resultData;
    }

    @Override
    public ResultData update(User user) {
        int i = userDao.update(user);
        ResultData resultData = new ResultData();
        List<User> list = new ArrayList<User>();
        if(i>0){
            resultData.setCode(1);
            resultData.setMsg("成功");
            list.add(user);
            resultData.setData(list);
        }else{
            resultData.setCode(0);
            resultData.setMsg("失败");
            resultData.setData(null);
        }
        return resultData;
    }

    @Override
    public ResultData getAllUsers() {
        List<User> list = userDao.getAllUsers();
        ResultData resultData = new ResultData();
        resultData.setCode(200);
        resultData.setMsg("success");
        resultData.setData(list);
        return resultData;
    }

    @Override
    public ResultData getOne(int id) {
        System.out.println("查询的参数是："+id);
        List<User> u = userDao.getOne(id);
        System.out.println("查询到的数据是："+u);

        ResultData resultData = new ResultData();
        List<User> list = new ArrayList<>();


        if(u.size()>0){
            resultData.setCode(1);
            resultData.setMsg("成功");
            resultData.setData(u);
        }else{
            resultData.setCode(0);
            resultData.setMsg("失败");
            resultData.setData(null);
        }
        return resultData;
    }
}
